package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.common.BaseContext;
import org.example.common.R;
import org.example.entity.ShoppingCart;
import org.example.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
     @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
         log.info("购物车数据：{}",shoppingCart);
         Long currentId= BaseContext.getCurrentId();
         shoppingCart.setUserId(currentId);
         Long dishId=shoppingCart.getDishId();
         LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
         lambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

         if (dishId!=null){
             lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
         }else {
             lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
         }
         ShoppingCart shoppingCartOne=shoppingCartService.getOne(lambdaQueryWrapper);

         if (shoppingCartOne!=null){
             Integer num=shoppingCartOne.getNumber();
             shoppingCartOne.setNumber(num+1);
             shoppingCartService.updateById(shoppingCartOne);
         }else {
             shoppingCart.setNumber(1);
             shoppingCart.setCreateTime(LocalDateTime.now());
             shoppingCartService.save(shoppingCart);

             shoppingCartOne=shoppingCart;
         }
         return R.success(shoppingCartOne);
    }

    @GetMapping("/list")
     public R<List<ShoppingCart>> list(){
        log.info("购物车内容：{}");
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list=shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);

    }

    @DeleteMapping("/clean")
    public R<String> delete(){
         LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
         lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
         shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空购物车成功");

    }
    @PostMapping("/sub")
    public  R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId= BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        Long dishId=shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());

        if (dishId!=null){
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartOne=shoppingCartService.getOne(lambdaQueryWrapper);
        Integer num=shoppingCartOne.getNumber();
        if (num>1){
            shoppingCartOne.setNumber(num-1);
               shoppingCartService.updateById(shoppingCartOne);
        }else {
            shoppingCartService.remove(lambdaQueryWrapper);
            shoppingCartOne.setNumber(num - 1);
        }

            return R.success(shoppingCartOne);
        }
    }
